﻿

var mapPosition, mapOptions, map;
var weatherLayer, cloudLayer;
var sv = new google.maps.StreetViewService();
var panorama, panoramaOptions;
var getPoints;                                                             //Variable and Arrays(Dont set them in methods)
var getTextBoxValue;
var getTextBoxValue2;
var lookPosition;
var lookUpAndDownPosition;
var zoomPosition, zoomPositionMap;
var patternsArray = [];

//Arrays for panning,looking and zooming(created to fingd patterns)
var panLeft = ["Python left", "pon left", "uh left", "pan left", "han left", "on left", "haha left", "haha i left", "ha left", "ha i left", "hi im left", "hi left", "pine left", "on next", "han left", "Python last", "pa on left", "time left", "pine less"];
var panRight = ["Python right", "pine right", "pon right", "pon Royce", "pine Royce", "pan right", "Python Royce", "palm right", "Pomeroy"];
var lookUp = ["look up", "local opp", "milk open", "milk help", "milk hope", "milk pump", "look OTP", "look both", "local open", "Lok opp", "local OTP", "look old", "Lok open"];
var lookDown = ["look down", "milk down"];
var zoomIn = ["zoom in"];
var zoomOut = ["zoom out", "zoom else"];
var zoomInMap = ["zoom in map", "zoom in math"];
var zoomOutMap = ["zoom out map", "zoom else map", "zoom ELT map", "zoom and mop"];
var markersArray = [];

var findLookUp = "lo";
var lngpoint, latpoint, coOrd;
var updateCoordinates;

//Dictionaries for cities,stores,mapTypes and co-ordinate calculations designated for quicker retrievals
var cities = {
    "London": new google.maps.LatLng(51.50795, -0.12893), "Dublin": new google.maps.LatLng(53.34876, -6.25972),
    "open": new google.maps.LatLng(53.34876, -6.25972), "Tobin": new google.maps.LatLng(53.34876, -6.25972),
    "Paris": new google.maps.LatLng(48.85793, 2.29542), "New York": new google.maps.LatLng(40.75964, -73.98441),
    "Sydney": new google.maps.LatLng(-33.85923, 151.21351), "Madrid": new google.maps.LatLng(40.45214, -3.69089),
    "Amsterdam": new google.maps.LatLng(52.37010, 4.89627), "Munich": new google.maps.LatLng(48.14356, 11.57804),
    "Rome": new google.maps.LatLng(41.90020, 12.45650)
};
var storesAndResteraunts =
            {
                "restaurant": 'restaurant', "restaurants": 'restaurant', "stores": 'store',
                "stars": 'store', "star": 'store', "churches": 'church', "cafes": 'cafe', "Cafe": 'cafe'
                , "cafe": 'cafe', "charges": 'church'
            };

var mapModes =
            {
                "Sausalito": google.maps.MapTypeId.SATELLITE, "map": google.maps.MapTypeId.ROADMAP, "satellite": google.maps.MapTypeId.SATELLITE, "Southlake": google.maps.MapTypeId.SATELLITE,
                "subtly": google.maps.MapTypeId.SATELLITE, "cycle is": google.maps.MapTypeId.SATELLITE, "psychedelic": google.maps.MapTypeId.SATELLITE,
                "suck a lease": google.maps.MapTypeId.SATELLITE, "subtly to": google.maps.MapTypeId.SATELLITE, "sucks at least": google.maps.MapTypeId.SATELLITE,
                "terrain": google.maps.MapTypeId.TERRAIN, "to rain": google.maps.MapTypeId.TERRAIN, "Holly Bridget": google.maps.MapTypeId.HYBRID, "holy bread": google.maps.MapTypeId.HYBRID,
                "holy bridge": google.maps.MapTypeId.HYBRID, "Holly bridge": google.maps.MapTypeId.HYBRID

            };
var coOrdinateCalculations = [
                                            {
                                                "AlterLat": 0.000105,
                                                "AlterLng": 0.000163
                                            },
                                        {
                                            "AlterLat": 0.000135,
                                            "AlterLng": 0.0001
                                        }

                                        ];



//###############Commands#####################
// Pan Left,Pan Right
// Turn Left,Turn Right
// Look Up, Look Down
//Zoom In,Zoom Out,Zoom In Map,Zoom Out Map
// Go Forward,Go Back
//Turn Around  
//Go to (city)
//Show resteraunts,stores,churches
//Change map modes
//Show weather
//Save and load locations
//##########################################
//When Speech is Recieved
//##########################################
function append() {


    getTextBoxValue = document.getElementById("input").value;               //get the value in the text box once the speech is finished  
    getTextBoxValue2 = document.getElementById("input").value.toLowerCase();

    coOrd = panorama.getPosition();
    lngpoint = coOrd.lng();                                                 // gets the position of the street view 
    latpoint = coOrd.lat();
    getPoints = map.getCenter();

    lookUpAndDownPosition = panorama.getPov().pitch;
    lookPosition = panorama.getPov().heading;
    zoomPosition = panorama.getPov().zoom;
    zoomPositionMap = map.getZoom();

    findGoTo1InTextBox = getTextBoxValue.indexOf("go to");                 // look for specific values in the text box when you speak,retrieves a numeric value
    findLookUpInTextBox = getTextBoxValue2.indexOf(findLookUp);
    findMilkInTextBox = getTextBoxValue2.indexOf("milk");
    findCallToInTextBox = getTextBoxValue2.indexOf("call to");
    findGoTo2InTextBox = getTextBoxValue2.indexOf("goto");
    findZoomInTextBox = getTextBoxValue2.indexOf("zoom");
    findFallingInTextBox = getTextBoxValue2.indexOf("falling");
    findFindInTextBox = getTextBoxValue2.indexOf("find");
    findModInTextBox = getTextBoxValue2.indexOf("mod");
    findForwardInTextBox = getTextBoxValue2.indexOf("forward");
    findBackInTextBox = getTextBoxValue2.indexOf("ack");
    findTurnInTextBox = getTextBoxValue2.indexOf("turn");
    findLocationInTextBox = getTextBoxValue2.indexOf("location");
    findSawAndInTextBox = getTextBoxValue2.indexOf("saw and");
    findPhoneAndInTextBox = getTextBoxValue2.indexOf("phone and");
    findWeatherInTextBox = getTextBoxValue2.indexOf("weather");



    /*patternsArray.push(findLocationInTextBox);
    var storeFun = [saveLoc()];
    if (patternsArray != -1) 
    {
    storeFun[0];

    }*/

    lookPostion = lookPosition.toFixed(0); //0 decimal places
    if (lookPosition < 0) {
        lookPosition = lookPosition + 360;
    }

    //###################################################
    //            SAVING AND LOADING LOCATIONS
    //###################################################
    if (findLocationInTextBox != -1) {
        //save to co-ordinates to local storage or load them from local storage depending on the command issued
        if (getTextBoxValue.substring(0, 4) == "load") {

            updateCoordinates = new google.maps.LatLng(localStorage.latitude, localStorage.longtitude);
            ModifyLocation(updateCoordinates, 90, 10);

            document.getElementById("textBox").value += "points stored are:" + localStorage.latitude + " " + localStorage.longtitude;
        }
        else if (getTextBoxValue.substring(0, 4) == "save") {
            localStorage.latitude = latpoint;
            localStorage.longtitude = lngpoint;
            accessDialog("You have successfully saved your location.To retrieve your saved location say Load Location !!", "Successful Notification");

        }

    }
    //################################################
    //GO FORWARD OR BACKWARDS
    //################################################
    else if (findForwardInTextBox != -1 || findBackInTextBox != -1) {


        //Going Forward
        if (findForwardInTextBox != -1) {

            goingForward();
        }
        //Going Back
        else {
            goingBack();
        }

        sv.getPanoramaByLocation(updateCoordinates, 50, processSVData);

    }
    //#########################
    //TURN LEFT,RIGHT OR AROUND
    //#########################
    else if (findTurnInTextBox != -1) {
        var findLeft = getTextBoxValue2.indexOf("left");
        var findRight = getTextBoxValue2.indexOf("right");
        var findAround = getTextBoxValue2.indexOf("around");
        //turn left
        if (findLeft != -1) {

            turnLeft(); //co-ordinates updated here
            sv.getPanoramaByLocation(updateCoordinates, 50, processSVData); //find the nearest panorama based on the new co-ordinates
            lookPosition = lookPosition - 90;

            if (lookPosition < 0) {

                lookPosition = lookPosition + 360;
            }
            document.getElementById("textBox").value += document.getElementById("input").value;
            ModifyHeadingOrPitch(lookPosition, lookUpAndDownPosition, zoomPosition);
        }
        //turn right
        else if (findRight != -1) {
            turnRight();
            sv.getPanoramaByLocation(updateCoordinates, 50, processSVData);
            lookPosition = lookPosition + 90;

            if (lookPosition > 360) {

                lookPosition = lookPosition - 360;
            }

            ModifyHeadingOrPitch(lookPosition, lookUpAndDownPosition, zoomPosition);
        }
        else {

            lookPosition += 180;
            if (lookPosition > 360) {
                lookPosition -= 360;

            }

            ModifyHeadingOrPitch(lookPosition, lookUpAndDownPosition, zoomPosition);
        }


    }
    //###########################################
    //CHANGE MAP TYPE
    //###########################################
    else if (findModInTextBox != -1) {

        var findMapType = getTextBoxValue.substring(0, findModInTextBox - 1);
        document.getElementById("textBox").value += findMapType;

        //retrieve map type from the dictionary
        var getMapType = mapModes[findMapType];
        document.getElementById("textBox").value += getMapType + "   ";
        if (getMapType === undefined) {
            accessDialog("You have declared an incorrect mapType.Please issue your instructions clearly!!", "Warning Notification");
        }
        else {
            modifyMapType(getMapType);
        }
    }
    //###########################################
    //    FIND STORES
    //###########################################
    else if (findFallingInTextBox != -1 || findFindInTextBox != -1 || findSawAndInTextBox != -1
    || findPhoneAndInTextBox != -1) {

        var findPlaceTypes, getStore;
        if (findFallingInTextBox != -1) {
            findPlaceTypes = getTextBoxValue.substring(8);
        }
        else {
            findPlaceTypes = getTextBoxValue.substring(5);
        }
        document.getElementById("textBox").value += findPlaceTypes;

        getStore = storesAndResteraunts[findPlaceTypes];

        findPlaces(getStore);

    }
    //############################################################
    //Display Weather
    //############################################################
    else if (findWeatherInTextBox != -1) {
        if (getTextBoxValue.substring(0, 4) == "show") {
            weatherLayer = new google.maps.weather.WeatherLayer({
                temperatureUnits: google.maps.weather.TemperatureUnit.CELSIUS
            });
            weatherLayer.setMap(map);

            cloudLayer = new google.maps.weather.CloudLayer();
            cloudLayer.setMap(map);
        }
        else if (getTextBoxValue.substring(0, 6) == "remove") {
            weatherLayer.setMap(null);
            cloudLayer.setMap(null);


        }

    }

    //############################################################
    //ZOOM IN OR OUT STREET VIEW
    //############################################################

    else if (findZoomInTextBox != -1) {

        if (contains(getTextBoxValue, zoomIn)) {
            zoomPosition += 1;
            if (zoomPosition > 3) {
                accessDialog("You cannot zoom in any further!!", "Warning Notification");
                zoomPosition = 3;

            }
            else {
                ModifyHeadingOrPitch(lookPosition, lookUpAndDownPosition, zoomPosition);
            }
        }
        else if (contains(getTextBoxValue, zoomOut)) {
            zoomPosition -= 1;
            if (zoomPosition < 0) {
                accessDialog("You cannot zoom out any further!!", "Warning Notification");
                zoomPosition = 0;

            }
            else {
                ModifyHeadingOrPitch(lookPosition, lookUpAndDownPosition, zoomPosition);
            }
        }
        //############################################################
        //ZOOM IN OR OUT ROADMAP
        //############################################################

        else if (contains(getTextBoxValue, zoomInMap)) {

            zoomPositionMap += 2;
            if (zoomPositionMap > 22) {
                accessDialog("You cannot zoom in any further on the map!!", "Warning Notification");
                zoomPositionMap = 22;

            }
            else {
                ZoomInOutMap(zoomPositionMap);
            }

        }
        else if (contains(getTextBoxValue, zoomOutMap)) {

            zoomPositionMap -= 2;
            if (zoomPositionMap < 0) {
                accessDialog("You cannot zoom out any further on the map!!", "Warning Notification");
                zoomPositionMap = 0;

            }
            else {
                ZoomInOutMap(zoomPositionMap);
            }

        }
        else {
            document.getElementById("textBox").value += document.getElementById("input").value;
        }

    }
    //############################################################
    //GO TO A PARTICULAR CITY
    //############################################################
    else if (findGoTo1InTextBox != -1 || findCallToInTextBox != -1 || findGoTo2InTextBox != -1) {

        var getCityFromTextBox;
        if ((findGoTo1InTextBox != -1)) {
            getCityFromTextBox = getTextBoxValue.substring(6);
        }
        else if (findCallToInTextBox != -1) {
            getCityFromTextBox = getTextBoxValue.substring(8);
        }
        else {
            getCityFromTextBox = getTextBoxValue.substring(5);
        }
        document.getElementById("textBox").value += getCityFromTextBox;
        updateCoordinates = cities[getCityFromTextBox];

        if (latpoint.toFixed(3) == updateCoordinates.lat().toFixed(3) && lngpoint.toFixed(3) == updateCoordinates.lng().toFixed(3)) {

            accessDialog("You are already in this city!!", "Warning Notification");
        }
        else {
            ModifyLocation(updateCoordinates, 90, 10);
        }


    }

    //#################################################################
    //LOOKING UP AND DOWN ON THE MAP
    //##################################################################

    else if ((findLookUpInTextBox != -1 && findLookUpInTextBox < 3) || findMilkInTextBox != -1) {
        //LOOKING UP
        if (contains(getTextBoxValue, lookUp)) {
            lookUpAndDownPosition += 20;
            if (lookUpAndDownPosition > 90) {
                accessDialog("You cannot look up any further!!", "Warning Notification");
            }
            else {

                document.getElementById("textBox").value += document.getElementById("input").value + " worked";
                ModifyHeadingOrPitch(lookPosition, lookUpAndDownPosition, zoomPosition);
            }
        }
        //LOOKING DOWN
        else if (contains(getTextBoxValue, lookDown)) {
            lookUpAndDownPosition -= 20;
            if (lookUpAndDownPosition < -90) {
                accessDialog("You cannot look down any further!!", "Warning Notification");

            }
            else {
                document.getElementById("textBox").value += document.getElementById("input").value + " worked";
                ModifyHeadingOrPitch(lookPosition, lookUpAndDownPosition, zoomPosition);
            }

        }
        else {
            document.getElementById("textBox").value += document.getElementById("input").value + " there";

        }


    }

    //###########################################################
    //PAN LEFT OR RIGHT
    //###########################################################
    else if (contains(getTextBoxValue, panLeft)) //if the user wishes to look left without going anywhere
    {
        lookPosition = lookPosition - 60;

        if (lookPosition < 0) {

            lookPosition = lookPosition + 360;                //position should only be from 0 - 360
        }
        document.getElementById("textBox").value += document.getElementById("input").value;
        ModifyHeadingOrPitch(lookPosition, lookUpAndDownPosition, zoomPosition);     //reset the map
        document.getElementById("textBox").value += document.getElementById("input").value + " worked";



    }

    else if (contains(getTextBoxValue, panRight)) {

        lookPosition = lookPosition + 60;

        if (lookPosition > 360) {

            lookPosition = lookPosition - 360;
        }
        document.getElementById("textBox").value += document.getElementById("input").value;
        ModifyHeadingOrPitch(lookPosition, lookUpAndDownPosition, zoomPosition);
        document.getElementById("textBox").value += document.getElementById("input").value + " worked";

    }

    else {
        document.getElementById("textBox").value += document.getElementById("input").value + " error";     //This is only code for testing purposes
        // to see what has been said when it is not recognized
    }

}



//############################################################
//           INIATILIZING MAP AND STREET VIEW
//#############################################################
var placesList;

function initialize(point1, point2, heading, pitch) {
    $("#dialog").dialog({ autoOpen: false });  //works when loading
    //$('#tool').hide();
    //$('#results').hide();
    // $('#Error').show();
    $("#check").append("<strong>Error</strong>"); //another option For error display

    mapPosition = new google.maps.LatLng(point1, point2);
    mapOptions =
            {
                center: mapPosition,                             //object which stores the original map settings
                zoom: 7,
                mapTypeId: 'roadmap',
                streetViewControl: true

            };
    map = new google.maps.Map(
            document.getElementById('map_canvas'), mapOptions);

    map.setTilt(45);


    //#################################################
    // Street View which is linked to map
    //##################################################

    panoramaOptions =
            {
                position: mapPosition,
                pov:
                {
                    heading: heading,
                    pitch: pitch,
                    zoom: 1
                }
            };

    panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'), panoramaOptions);
    map.setStreetView(panorama);

    google.maps.event.addListener(panorama, 'position_changed', function () {
        var positionCell = document.getElementById('position');
        positionCell.firstChild.nodeValue = panorama.getPosition();
    });
    google.maps.event.addListener(panorama, 'pov_changed', function () {
        var headingCell = document.getElementById('headingCell');

        headingCell.firstChild.nodeValue = panorama.getPov().heading;

    });
}










